//
//  oldSuspendTopOrderView.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/28.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldSuspendTopOrderView.h"
#import "oldNetWork.h"
#import "oldHub.h"
#import "AppDelegate.h"
#import "oldMessageManager.h"
#import "oldMessageAlertView.h"

@interface oldSuspendTopOrderView()<oldMessageDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label_title;
@property (nonatomic, strong)oldOrderInfo *orderInfo;
@property (nonatomic, assign)BOOL ifSearching;
@property (nonatomic, assign)NSInteger number;
@property (nonatomic, assign)NSInteger count;
@property (nonatomic, strong)oldMessageManager *manager;
@property (nonatomic, strong)oldMessageManager *managerEnd;
@property (nonatomic, strong)oldMessageManager *managerFailed;
@property (nonatomic, strong)oldMessageManager *managerReject;
@property (nonatomic, strong)oldMessageManager *managerCancel;
@end

@implementation oldSuspendTopOrderView

- (void)awakeFromNib
{
    [super awakeFromNib];
    UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesView:)];
    [self addGestureRecognizer:gesture];
    self.layer.cornerRadius = 19.f;
    self.layer.masksToBounds = YES;
    [self initMessage];
}

- (void)initMessage
{
    _manager = [[oldMessageManager alloc] initWithType:orderCreate];
    _manager.delegate = self;
    [_manager registerForNotifications];
    
    _managerEnd = [[oldMessageManager alloc] initWithType:orderEnd];
    _managerEnd.delegate = self;
    [_managerEnd registerForNotifications];
    
    _managerFailed = [[oldMessageManager alloc] initWithType:orderFail];
    _managerFailed.delegate = self;
    [_managerFailed registerForNotifications];
    
    _managerReject = [[oldMessageManager alloc] initWithType:orderReject];
    _managerReject.delegate = self;
    [_managerReject registerForNotifications];
    
    _managerCancel = [[oldMessageManager alloc] initWithType:orderCancel];
    _managerCancel.delegate = self;
    [_managerCancel registerForNotifications];
}

#pragma mark - message
- (void)receiveMessage:(id)message type:(oldMessageType)type
{
    [self refreshData];
    
    [[oldMessageAlertView shareInstance] showLabelWithString:message];
    
    if (type == orderCreate || type == orderEnd) {
        //        [oldHub waiting:@"获取订单中信息.."];
    }
    else
    {
        [self.delegate orderFailed:message];
    }
}

- (void)gesView:(id)sender
{
    [self.delegate SuspendTopViewTouched:_orderInfo count:_count number:_number];
}

- (void)setCount:(NSInteger)count number:(NSInteger)number
{
    _number = number;
    _count = count;
}

- (void)refreshData
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_Family_GetCurrentOrder params:nil success:^(id data) {
        [weakSelf initOrder:data];
    } error:^(NSString *error) {
        [oldHub warningWindow:error];
    }];
}

- (void)addToWindow
{
    self.frame = CGRectMake(SCREEN_WIDTH - 120, 100, 137, 38);
    [[AppDelegate getMainWindow] addSubview:self];
}

- (void)initOrder:(id)data
{
    if ([data isKindOfClass:[NSArray class]]) {
        self.orderInfo = nil;
        self.ifSearching = YES;
    }
    else if(data && ![data isKindOfClass:[NSNull class]])
    {
        self.orderInfo = [[oldOrderInfo alloc] initWithDictionary:data];
        self.ifSearching = NO;
    }
    else
    {
        self.orderInfo = nil;
        self.ifSearching = NO;
    }
    
    [self initView];
}

- (void)initView
{
    BOOL ifremove = NO;
    
    if (_orderInfo) {
        _label_title.text = @"订单进行中";
        _number = 0;
        _count = 0;
    }
    else if (_ifSearching && !_orderInfo)
    {
        _label_title.text = @"订单等待中";
    }
    else
    {
        _number = 0;
        _count = 0;
        ifremove = YES;
    }
    
    [self.delegate ifNeedRemove:ifremove];
}


@end
